﻿using System;
using System.ComponentModel;
using System.Drawing;

using System.Windows.Forms;
using RayDen.SceneLab.Components;
using RayDen.SceneLab.Controllers.FormControllers;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;

namespace RayDen.SceneLab.UI
{
    public partial class ImageEditorForm : Form
    {
        protected ImageEditorController Controller;
        public ImageEditorForm()
        {
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Controller = new ImageEditorController(this);

        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dlgOpenImage.ShowDialog() == DialogResult.OK)
            {
                this.Controller.OpenFile(dlgOpenImage.FileName);
                this.groupBox1.Text = this.Controller.ImageName;
                this.ctlCanvas.Image = new Bitmap(this.Controller.Image);
                //this.ctlCanvas.Refresh();
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (this.IsDisposed)
                return;
            if (m.HWnd != this.Handle)
            {
                return;
            }
            switch (m.Msg)
            {
                case 0x020E: //Win32Messages.WM_MOUSEHWHEEL:
                    FireMouseHWheel(m.WParam, m.LParam);
                    m.Result = (IntPtr)1;
                    break;
                default:
                    break;
            }
        }

        private void FireMouseHWheel(IntPtr intPtr1, IntPtr intPtr2)
        {
            var w = intPtr1.ToInt32();
            var l = intPtr2.ToInt32();
            MessageBox.Show("WHeel" + w + " : " + l, "Wheel event");
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.Controller.Dispose();
            base.OnClosing(e);
        }

        private void ctlCanvas_MouseHover(object sender, EventArgs e)
        {
        }

        private void ctlCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            this.toolStripStatusLabel1.Text = string.Format("At {0};{1}", e.X, e.Y);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (dlgOpenImage.ShowDialog() == DialogResult.OK)
            {
                this.Controller.OpenEthalonFile(dlgOpenImage.FileName);
                Logger.WriteLine("Opened ethalon image file " + dlgOpenImage.FileName);
            }
        }

        private void btnEvaluateError_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.Controller.EvaluateError(), "Image Variance Evaluation", MessageBoxButtons.OK);
        }
    }

}
